﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SIMS.School;
using SIMS.Global;
using SIMS.Company;
using System.Text;

public partial class School_Period_CreateEdit : System.Web.UI.Page
{

    private void BindCourseDetail()
    {
        ddlCourseDetailList.Items.Clear();
        ddlCourseDetailList.Items.Add(new ListItem("Select", "-1"));

        CourseDetailList _courseDetailList = new CourseDetailList();
        CourseDetailDB _courseDetailDB = new CourseDetailDB();
        _courseDetailList = _courseDetailDB.GetCourseDetailList(Convert.ToInt32(hidCompanyId.Value));
        if (_courseDetailList != null && _courseDetailList.Count > 0)
        {
            foreach (CourseDetail _cd in _courseDetailList)
            {
                ddlCourseDetailList.Items.Add(new ListItem(_cd.Name, _cd.Id.ToString()));
            }
        }
    }

    private void ShowCourseDetail()
    {
        CourseDetail _courseDetail = new CourseDetail();
        CourseDetailDB _courseDetailDB = new CourseDetailDB();
        _courseDetail = _courseDetailDB.GetCourseDetail(Convert.ToInt32(ddlCourseDetailList.Text));

        if (_courseDetail != null && _courseDetail.Id > 0)
        {
            StringBuilder _strCourseDetail = new StringBuilder();
            _strCourseDetail.Append("<table cellpadding='3' cellspacing='4'>");
            _strCourseDetail.Append("<tr><td>Course:</td><td>" + _courseDetail.CourseName + "</td></tr>");
            _strCourseDetail.Append("<tr><td>Standard Level:</td><td>" + _courseDetail.StandardLevelName + "</td></tr>");
            _strCourseDetail.Append("<tr><td>Major Field:</td><td>" + _courseDetail.MajorFieldName + "</td></tr>");
            _strCourseDetail.Append("</table>");

            lblCourseDetail.Text = _strCourseDetail.ToString();
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session[Global.CompanyId] != null)
            {
                hidCompanyId.Value = Session[Global.CompanyId].ToString();
                lblCompany.Text = Session[Global.CompanyName].ToString();

                BindCourseDetail();

                if (Request.Params["id"] != null)
                {
                    int _PeriodId = 0;
                    if (int.TryParse(Request.Params["id"].ToString(), out _PeriodId) && _PeriodId > 0)
                    {
                        Period _Period = new Period();
                        PeriodDB _PeriodDB = new PeriodDB();
                        _Period = _PeriodDB.GetPeriod(_PeriodId);

                        if (_Period.Id > 0)
                        {
                            if (_Period.Error.ErrorNo > 0)
                            {
                                lblMessage.Text = _Period.Error.ErrorMessage;
                            }
                            else
                            {
                                hidPeriodId.Value = _PeriodId.ToString();
                                txtPeriodName.Text = _Period.Name;
                                txtDescription.Text = _Period.Description;
                                txtStartDate.Text = _Period.StartDate.ToString("dd-MMM-yyyy");
                                txtEndDate.Text = _Period.EndDate.ToString("dd-MMM-yyyy");
                                ddlCourseDetailList.Text = _Period.CourseDetailId.ToString();
                                ddlCourseDetailList_SelectedIndexChanged(sender, e);
                            }
                        }
                    }
                    else
                    {
                        lblMessage.Text = "Invalid parameter!";
                    }
                }
            }
            else
            {
                lblMessage.Text = "No company found!";
                btnSubmit.Enabled = false;
            }
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            if (Page.IsValid)
            {
                DateTime _startDate = new DateTime();
                DateTime _endDate = new DateTime();

                if (!DateTime.TryParse(txtStartDate.Text, out _startDate))
                {
                    lblMessage.Text = "Start date is invalid.";
                    return;
                }

                if (!DateTime.TryParse(txtEndDate.Text, out _endDate))
                {
                    lblMessage.Text = "End date is invalid.";
                    return;
                }

                if (DateTime.Compare(_startDate, _endDate) > 0)
                {
                    lblMessage.Text = "Start date should be (earlier than or equal to) end date.";
                    return;
                }

                Period Period = new Period();
                PeriodDB _PeriodDB = new PeriodDB();
                int _PeriodId = 0;

                if (int.TryParse(hidPeriodId.Value, out _PeriodId) && _PeriodId > 0)
                {
                    Period = _PeriodDB.GetPeriod(_PeriodId);
                }
                else
                {
                    Period.Id = 0;
                    Period.Error = new MyError();
                }

                Period.Name = txtPeriodName.Text;

                if (txtDescription.Text.Trim().Length > 0)
                {
                    Period.Description = txtDescription.Text;
                }
                else
                {
                    Period.Description = string.Empty;
                }

                Period.StartDate = _startDate;
                Period.EndDate = _endDate;

                Period.CourseDetailId = Convert.ToInt32(ddlCourseDetailList.Text);
                Period.IsActive = false;

                _PeriodDB.Save(Period);

                if (Period.Error.ErrorNo > 0)
                {
                    if (Period.Error.ErrorNo == 1)
                    {
                        lblMessage.Text = Period.Error.ErrorMessage;
                    }
                    else
                    {
                        lblMessage.Text = "(" + Period.Error.ErrorLocation + ")";
                        lblMessage.Text += Period.Error.ErrorMessage;
                        lblMessage.Text += "<br/><br/>" + Period.Error.ErrorStack;
                    }
                }
                else
                {
                    lblMessage.Text = "Saved successfully!";
                    Response.Redirect("default.aspx?companyid=" + hidCompanyId.Value);
                }
            }
            else
            {
                lblMessage.Text = "Invalid form.";
            }
        }
        catch (Exception Ex)
        {
            lblMessage.Text = Ex.Message;
            MyError.CreateExceptionLog(Ex, "Company_CreateEdit");
        }

    }

    protected void ddlCourseDetailList_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlCourseDetailList.Items.Count > 0 && ddlCourseDetailList.SelectedIndex > 0)
        {
            ShowCourseDetail();
        }
        else
        {
            lblCourseDetail.Text = "Please select course detail.";
        }
    }
}